home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1334_pw_check.c < prev    next >
Text File  |  1993-09-23  |  1KB  |  61 lines

  1. /* 
  2.  * Copyright 1993 NeXT Computer Inc.  All rights reserved.
  3.  * Written by Marc Majka
  4.  
  5.  * You may freely copy, distribute and reuse the code in this example.  
  6.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  7.  * its fitness for any particular use.
  8.  */
  9.  
  10. #import <stdio.h>
  11. #import <pwd.h>
  12. #import <signal.h>
  13. #import <sys/reboot.h>
  14.  
  15. #define MAXATTEMPTS 3
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     char *crypt(), *getpass(), salt[3], key[9], target[16];
  22.     struct passwd *pw;
  23.     int trying, attempt;
  24.  
  25.     /* ignore signals */
  26.     signal(SIGINT,  SIG_IGN);
  27.     signal(SIGQUIT, SIG_IGN);
  28.     signal(SIGTERM, SIG_IGN);
  29.     signal(SIGTSTP, SIG_IGN);
  30.  
  31.     if (argc < 2) {                    /* no command line option */
  32.         pw = getpwuid(0);            /* lookup root */
  33.         if (pw == NULL) exit(0);        /* root doesn't exist! */
  34.     }
  35.  
  36.     else {
  37.         pw = getpwnam(argv[1]);            /* lookup given user name */
  38.         if (pw == NULL) {            /* user doesn't exist */
  39.             pw = getpwuid(0);        /* lookup root instead */
  40.             if (pw == NULL) exit(0);    /* root doesn't exist! */
  41.         }
  42.     }
  43.  
  44.     strcpy(target,pw->pw_passwd);        /* target password */
  45.  
  46.     salt[0] = target[0];            /* the "salt" */
  47.     salt[1] = target[1];
  48.     salt[2] = '\0';
  49.  
  50.     trying = 1;
  51.     for (attempt = 0; attempt < MAXATTEMPTS && trying; attempt++) {
  52.         strcpy(key, getpass("Password: "));
  53.         if (key[0] != 0 && !strcmp(target, crypt(key, salt))) 
  54.             trying = 0;
  55.     }
  56.  
  57.     if (trying) reboot(RB_HALT);        /* user was still trying - halt */
  58.     exit(0);                /* user entered the correct password */
  59. }
  60.         
  61.